# Minimum required CMake version
cmake_minimum_required(VERSION 3.18)

# Project name
project(examples VERSION 0.0.1)

# Language Standards ----------------------------------------------------------
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Append CMake Sub-Module from cmake Directory --------------------------------
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# Find Depends Libs  ----------------------------------------------------------
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

# Build Internal Utils Libs  --------------------------------------------------
set(MODEL_SESSION_DIR ${CMAKE_CURRENT_SOURCE_DIR}/model_session)
set(MODEL_SESSION_SRC ${MODEL_SESSION_DIR}/manager/manager.cpp
                      ${MODEL_SESSION_DIR}/session/session.cpp
                      ${MODEL_SESSION_DIR}/session/warp_affine.cpp
                      ${MODEL_SESSION_DIR}/model/model.cpp)

set(MODEL_SESSION_UTILS_DIR ${MODEL_SESSION_DIR}/utils)
set(MODEL_SESSION_UTILS_SRC ${MODEL_SESSION_UTILS_DIR}/file/file_handling.cpp
                            ${MODEL_SESSION_UTILS_DIR}/image_processing/image_process.cpp)

set(MODULE_SRC_NPU ${CMAKE_CURRENT_SOURCE_DIR}/cv/detection/yolov10.cpp)
set(MODULE_SRC_CPU ${CMAKE_CURRENT_SOURCE_DIR}/cv/detection/yolov10_cpu.cpp)

# Build Examples for NPU ------------------------------------------------------
option(BUILD_EXAMPLES_NPU "Build examples for NPU" ON)
if(BUILD_EXAMPLES_NPU)

    find_package(Python COMPONENTS Interpreter REQUIRED)

    execute_process(
        COMMAND ${Python_EXECUTABLE} -c "import site; print(site.getsitepackages()[0])"
        OUTPUT_VARIABLE PYTHON_SITE_PACKAGES_DIR
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )

    set(txdart_DIR ${PYTHON_SITE_PACKAGES_DIR}/pytxda/include)

    find_library(txdart_lib NAMES txdart PATHS ${PYTHON_SITE_PACKAGES_DIR}/pytxda/lib NO_DEFAULT_PATH)
    if(NOT txdart_lib)
        message(FATAL_ERROR "Could not find library libtxdart.so")
    endif()

    find_library(tx8_runtime_lib NAMES tx8_runtime PATHS ${PYTHON_SITE_PACKAGES_DIR}/pytxda/lib NO_DEFAULT_PATH)
    if(NOT tx8_runtime_lib)
        message(FATAL_ERROR "Could not find library libtx8_runtime.so")
    endif()

    set(MODULE_NAME yolov10_sample_npu)
    add_executable(${MODULE_NAME} ${MODULE_SRC_NPU} ${MODEL_SESSION_UTILS_SRC} ${MODEL_SESSION_SRC})
    target_compile_options(${MODULE_NAME} PRIVATE -Wall -Wextra -Werror -O3)
    target_link_libraries(${MODULE_NAME} PRIVATE ${OpenCV_LIBS} ${txdart_lib} ${tx8_runtime_lib})
    target_include_directories(${MODULE_NAME} PRIVATE ${MODEL_SESSION_DIR} ${txdart_DIR})
endif()

# Build Examples for CPU ------------------------------------------------------
option(BUILD_EXAMPLES_CPU "Build examples for CPU" OFF)
if(BUILD_EXAMPLES_CPU)
    find_library(ORT_LIB NAMES onnxruntime libonnxruntime.so.1.21.1 PATHS ${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime/build/Linux/Release NO_DEFAULT_PATH)
    set(MODULE_NAME yolov10_sample_cpu)
    set(ORT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime/include/onnxruntime/core/session)
    add_executable(${MODULE_NAME} ${MODULE_SRC_CPU} ${MODEL_SESSION_UTILS_SRC} ${MODEL_SESSION_SRC})
    target_compile_options(${MODULE_NAME} PRIVATE -Wall -Wextra -Werror -O3)
    target_link_libraries(${MODULE_NAME} PRIVATE ${OpenCV_LIBS} ${ORT_LIB})
    target_include_directories(${MODULE_NAME} PRIVATE ${MODEL_SESSION_DIR} ${ORT_INCLUDE_DIR})
endif()
